<?php
namespace app\admin\controller;
use think\Db;
class Article  extends  Common
{
    //文章列表
    public function articleList()
    {
        $list = DB::name('article')->paginate(6);//分页
        $page = $list->render();
        $this->assign('article',$list);//将分类信息分配到页面上
        $this->assign('page',$page);//将页码分配到页面上
        return  view();
    }
    /*
     *
     * 添加文章
     *
     * */

    public function  addArticle(){
        $category = DB::name('category')->order('cid desc')->select();
        $this->assign('category',$category);
        return  view();//显示添加文章的页面 (视图)
    }


    public   function  handle(){
        $obj =DB::name('article');//连接数据库中的category 表
        $inputs =input();//接收post提交过来的数据
        $data=array();
        $data['article_title'] =$inputs['article_title'];
        $data['article_abstract'] =$inputs['article_abstract'];
        $data['article_content']  =$inputs['article_content'];
        $data['cid']  =$inputs['cid'];
        $data['add_time']  =time();
        if(empty($data['article_title'])){
            return array('code'=>0,'msg'=>'文章标题不能为空');
        }
        if(empty($data['cid'])){
            return array('code'=>0,'msg'=>'文章分类不能为空');
        }
        if(empty($data['article_abstract'])){
            return array('code'=>0,'msg'=>'文章摘要不能为空');
        }
        if(empty($data['article_content'])){
            return array('code'=>0,'msg'=>'文章内容不能为空');
        }
        $res =$obj->insert($data);  // "insert  into blog_category  values(ssss,ddd,,fff)"
        if($res){
            return array('code'=>1,'msg'=>'添加成功');
        }else{
            return array('code'=>0,'msg'=>'添加失败');
        }
    }

    public  function editCategory(){
        $input=input();
        $result = Db::name('category')->where('cid','=',$input['cid'])->find();
        $this->assign('category',$result);//把$result这个数组分配到页面上,页面上使用的时候用$category
        return view();
    }


    public  function  editHandle(){
        $input=input();
        $map['cname']=$input['cname'];
        $res = Db::name('category')->where('cid','=',$input['cid'])->update($map);
        if($res){
            return array('code'=>1,'msg'=>'修改成功');
        }else{
            return array('code'=>0,'msg'=>'修改失败');
        }

    }


    public  function  deleteCategory(){
        $input=input();
        $res = DB::name('category')->where('cid','=',$input['cid'])->delete();
        if($res){
            return array('code'=>1,'msg'=>'删除成功');
        }else{
            return array('code'=>0,'msg'=>'删除失败');
        }
    }


}
